home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
1833
/
1833.xpi
/
chrome
/
yoono.jar
/
content
/
yoono
/
bookmarks
/
bookmarksio.js
< prev
next >
Wrap
Text File
|
2009-12-16
|
6KB
|
217 lines
/*
* bookmarkio.js
*
* @author david marteau <marteau.david@free.fr>
* @copyright 2005-2006 Yoono
*/
var ynBookmarksIODrivers =
{
// XBEL
xbel: {
toXML : function() {
return (new ynXBELIOService()).exportBookmarks();
},
fromXML : function( xmlNode, baseURI ) {
new ynXBELIOService().importBookmarks(xmlNode,null);
},
get fileExt() { return "xbel"; }
},
// OPML2
opml: {
toXML : function() {
var ownerName;
var ownerEmail;
var ownerId;
return (new ynOPMLIOService()).exportBookmarks(ownerName,ownerEmail,ownerId );
},
fromXML : function( xmlNode, baseURI ) {
new ynOPMLIOService().importBookmarks(xmlNode,null,baseURI);
},
get fileExt() { return "opml"; }
}
};
var ynBookmarksIO =
{
_preference : "extensions.yoono.bookmark.exportdir",
_bundle : null,
_getFile : function( title, mode, defaultString )
{
const nsIFilePicker = Components.interfaces.nsIFilePicker;
mode = (mode=="modeOpen"?
nsIFilePicker.modeOpen:
nsIFilePicker.modeSave);
var prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
var fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(window,title, mode);
if(mode == nsIFilePicker.modeOpen)
{
fp.appendFilter(this.getString("xbelfiles") ,"*.xbel");
fp.appendFilter(this.getString("opmlfiles") ,"*.opml");
fp.appendFilters(fp.filterXML);
}
fp.appendFilters(fp.filterAll);
if(defaultString)
fp.defaultString = defaultString;
var dir = null;
try {
dir = prefs.getComplexValue(this._preference,Components.interfaces.nsILocalFile);
} catch (e) {}
// fallback to home dir
if(dir==null) {
dir = Components.classes["@mozilla.org/file/directory_service;1"].
getService(Components.interfaces.nsIProperties).get("Home",Components.interfaces.nsIFile);
}
if(dir && dir.exists())
fp.displayDirectory = dir;
var ret = fp.show();
if( ret == nsIFilePicker.returnOK ||
ret == nsIFilePicker.returnReplace)
{
var results = [];
var nsILocalFile = Components.interfaces.nsILocalFile;
var file = fp.file;
var dir = file.parent.QueryInterface(nsILocalFile);
prefs.setComplexValue(this._preference,Components.interfaces.nsILocalFile,dir);
return file;
}
return null;
},
getString : function( aString )
{
if(this._bundle == null) {
this._bundle = document.getElementById("yoono-bmks-bundle");
}
return this._bundle.getString(aString);
},
getExportType : function()
{
var args = {};
window.openDialog('chrome://yoono/content/bookmarks/exporttype.xul',
'', 'chrome,modal',args);
return args.value;
},
exportBookmarks : function()
{
const XML10_UTF8 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\n";
var exportType = this.getExportType();
if(!exportType)
return;
var driver = ynBookmarksIODrivers[exportType];
var title = this.getString("bookmarks.export.title");
var file = this._getFile(title,"modeSave","bookmarks."+driver.fileExt);
if(file)
{
var xbel = driver.toXML();
var fos = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(Components.interfaces.nsIFileOutputStream);
var xmlContent = xbel.toXMLString();
fos.init(file, -1, -1, false);
fos.write(XML10_UTF8,XML10_UTF8.length);
var converter = CL["@mozilla.org/intl/converter-output-stream;1"].createInstance( CI.nsIConverterOutputStream);
converter.init(fos, 'UTF-8', 1024, '-');
converter.writeString(xmlContent);
converter.flush();
fos.close();
}
},
importBookmarks : function()
{
var title = this.getString("bookmarks.import.title");
var file = this._getFile(title,"modeOpen","");
if(file)
{
//------------
// Read file
//------------
const MODE_RDONLY = 0x01;
const PERMS_FILE = 0644;
var fileStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
fileStream.init(file, MODE_RDONLY, PERMS_FILE, 0);
var sis = Components.classes["@mozilla.org/scriptableinputstream;1"].
createInstance(Components.interfaces.nsIScriptableInputStream);
sis.init(fileStream);
var parser = new DOMParser();
var xmldoc = parser.parseFromStream(fileStream, 'UTF-8', sis.available(),"text/xml");
fileStream.close();
sis.close();
// Get the uri from file
var ioSrvc = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var baseURI = ioSrvc.newFileURI(file);
var root = xmldoc.documentElement;
if ((root.tagName == "parsererror") ||
(root.namespaceURI == "http://www.mozilla.org/newlayout/xml/parsererror.xml"))
{
alert(this.getString("xmlparsererror"));
return;
}
var xmlnode = null;
var driver = null;
for( var type in ynBookmarksIODrivers) {
xmlnode = xmldoc.getElementsByTagName(type)[0];
if(xmlnode) {
driver = ynBookmarksIODrivers[type];
break;
}
}
if(!xmlnode) {
alert(this.getString("notvaliddocument"));
return;
}
var xmlstring = new XMLSerializer().serializeToString(xmlnode);
var xmlelement= new XML(xmlstring);
driver.fromXML(xmlelement,baseURI);
//alert(this.getString("bookmarksimported"));
}
}
}